Ext.define('Admin.view.bidding.BiddingController', {

    extend: 'Admin.ux.GridController',

    alias: 'controller.bidding',

    moreAction: function (grid, rowIndex, colIndex, item, e, record, row) {

        var rec = grid.getStore().getAt(rowIndex);

        var me = this;

        var treeMenu = new Ext.menu.Menu({
            shadow: "drop",
            allowOtherMenus: true,
            items: [
                new Ext.menu.Item({
                    text: "编辑",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        var win = Ext.create("Admin.ux.Window", {
                            title: '编辑',
                            width: 900,
                            height: 700,
                            animateTarget: row,
                            items: { xtype: 'bidding-edit' },
                            buttons: [
                                {
                                    text: '保存',
                                    ui: 'soft-green',
                                    iconCls: 'x-fa 	fa-floppy-o',
                                    handler: function (btn) {
                                        var form = btn.up("window").down("form").getForm();
                                        Ext.create('Admin.ux.Action').submit(form, '/tender/edit', me.getView(), win);
                                    }
                                },
                                {
                                    text: '重置',
                                    ui: 'soft-red',
                                    iconCls: 'x-fa 	fa-mail-reply',
                                    handler: function (btn) {
                                        var form = btn.up("window").down("form");
                                        form.getForm().load({
                                            url: '/tender/get',
                                            waitMsg: '加载中...',
                                            params: {
                                                id: rec.get('id')
                                            },
                                            success: function () {
                                                form.down('textfield[id=id]').setReadOnly(true);
                                            },
                                            failure: function (form, action) {
                                                if (action.result.msg != undefined) {
                                                    Ext.Msg.alert("提示", "<font color=red>" + action.result.msg + "!</font>");
                                                } else {
                                                    Ext.Msg.alert("提示", "<font color=red>加载出错,请刷新页面重试!</font>");
                                                }
                                            }
                                        });
                                        form.loadRecord(rec);
                                    }
                                },
                                {
                                    text: '取消',
                                    ui: 'gray',
                                    iconCls: 'x-fa 	fa-ban',
                                    handler: function (btn) {
                                        btn.up("window").close();
                                    }
                                }
                            ],

                            listeners: {
                                show: function (w, opts) {

                                    var form = this.down('form');

                                    form.getForm().load({
                                        url: '/tender/get',
                                        waitMsg: '加载中...',
                                        params: {
                                            id: rec.get('id')
                                        },
                                        success: function () {
                                            // form.down('textfield[name=id]').setReadOnly(true);
                                        },
                                        failure: function (form, action) {
                                            if (action.result.msg != undefined) {
                                                Ext.Msg.alert("提示", "<font color=red>" + action.result.msg + "!</font>");
                                            } else {
                                                Ext.Msg.alert("提示", "<font color=red>加载出错,请刷新页面重试!</font>");
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                }),
                new Ext.menu.Separator(),
                new Ext.menu.Item({
                    text: "预览",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        //自行修改
                        window.open("http://140.143.121.69/TenderWeb/ControlView/_ViewNotice?id=" + rec.get('id'));
                    }
                }),
                new Ext.menu.Separator(),
                new Ext.menu.Item({
                    text: "查看公告",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        var win = Ext.create("Admin.ux.Window", {
                            title: '查看公告',
                            width: 900,
                            height: 700,
                            animateTarget: row,
                            items: { xtype: 'bidding-edit' },
                            buttons: [{
                                text: '取消',
                                ui: 'gray',
                                iconCls: 'x-fa 	fa-ban',
                                handler: function (btn) {
                                    btn.up("window").close();
                                }
                            }
                            ],

                            listeners: {
                                show: function (w, opts) {

                                    var form = this.down('form');

                                    form.getForm().load({
                                        url: '/tender/get',
                                        waitMsg: '加载中...',
                                        params: {
                                            id: rec.get('id')
                                        },
                                        success: function () {
                                            form.down('textfield[id=id]').setReadOnly(true);
                                        },
                                        failure: function (form, action) {
                                            if (action.result.msg != undefined) {
                                                Ext.Msg.alert("提示", "<font color=red>" + action.result.msg + "!</font>");
                                            } else {
                                                Ext.Msg.alert("提示", "<font color=red>加载出错,请刷新页面重试!</font>");
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                }),
                new Ext.menu.Separator(),
                new Ext.menu.Item({
                    text: "报名管理",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        var win = Ext.create("Admin.ux.Window", {
                            title: '报名管理',
                            width: 700,
                            height: 500,
                            animateTarget: row,
                            items: { xtype: 'bidding-enroll' },
                            buttons: [{
                                text: '关闭',
                                ui: 'gray',
                                iconCls: 'x-fa 	fa-ban',
                                handler: function (btn) {
                                    btn.up("window").close();
                                }
                            }],

                            listeners: {
                                show: function (w, opts) {
                                    //手动刷新grid 的store
                                }
                            }
                        });
                    }
                }),
                new Ext.menu.Separator(),
                new Ext.menu.Item({
                    text: "网上竞价查看",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        Ext.Msg.alert('系统提示', '改功能尚未开放');
                        /* var win = Ext.create("Admin.ux.Window", {
                            title: '网上竞价查看',
                            width: 700,
                            height: 500,
                            animateTarget: row,
                            items: { xtype: 'bidding-enroll' },
                            buttons: [{
                                text: '关闭',
                                ui: 'gray',
                                iconCls: 'x-fa 	fa-ban',
                                handler: function (btn) {
                                    btn.up("window").close();
                                }
                            }],

                            listeners: {
                                show: function (w, opts) {
                                    //手动刷新grid 的store
                                }
                            }
                        }); */
                    }
                }),
                new Ext.menu.Separator(),
                new Ext.menu.Item({
                    text: "网上定标",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        var win = Ext.create("Admin.ux.Window", {
                            title: '网上定标',
                            width: 700,
                            height: 500,
                            animateTarget: row,
                            items: { xtype: 'bidding-enroll' },
                            buttons: [{
                                text: '关闭',
                                ui: 'gray',
                                iconCls: 'x-fa 	fa-ban',
                                handler: function (btn) {
                                    btn.up("window").close();
                                }
                            }],

                            listeners: {
                                show: function (w, opts) {
                                    //手动刷新grid 的store
                                }
                            }
                        });
                    }
                }),
                new Ext.menu.Separator(),
                new Ext.menu.Item({
                    text: "发布竞价结果公示",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        var win = Ext.create("Admin.ux.Window", {
                            title: '发布竞价结果公示',
                            width: 900,
                            height: 700,
                            animateTarget: row,
                            items: { xtype: 'bidding-edit' },
                            buttons: [
                                {
                                    text: '保存',
                                    ui: 'soft-green',
                                    iconCls: 'x-fa 	fa-floppy-o',
                                    handler: function (btn) {
                                        var form = btn.up("window").down("form").getForm();
                                        Ext.create('Admin.ux.Action').submit(form, '/tender/edit', me.getView(), win);
                                    }
                                },
                                {
                                    text: '重置',
                                    ui: 'soft-red',
                                    iconCls: 'x-fa 	fa-mail-reply',
                                    handler: function (btn) {
                                        var form = btn.up("window").down("form");
                                        form.getForm().load({
                                            url: '/tender/get',
                                            waitMsg: '加载中...',
                                            params: {
                                                id: rec.get('id')
                                            },
                                            success: function () {
                                                form.down('textfield[id=id]').setReadOnly(true);
                                            },
                                            failure: function (form, action) {
                                                if (action.result.msg != undefined) {
                                                    Ext.Msg.alert("提示", "<font color=red>" + action.result.msg + "!</font>");
                                                } else {
                                                    Ext.Msg.alert("提示", "<font color=red>加载出错,请刷新页面重试!</font>");
                                                }
                                            }
                                        });
                                        form.loadRecord(rec);
                                    }
                                },
                                {
                                    text: '取消',
                                    ui: 'gray',
                                    iconCls: 'x-fa 	fa-ban',
                                    handler: function (btn) {
                                        btn.up("window").close();
                                    }
                                }
                            ],

                            listeners: {
                                show: function (w, opts) {

                                    var form = this.down('form');

                                    form.getForm().load({
                                        url: '/tender/get',
                                        waitMsg: '加载中...',
                                        params: {
                                            id: rec.get('id')
                                        },
                                        success: function () {
                                            form.down('textfield[id=id]').setReadOnly(true);
                                        },
                                        failure: function (form, action) {
                                            if (action.result.msg != undefined) {
                                                Ext.Msg.alert("提示", "<font color=red>" + action.result.msg + "!</font>");
                                            } else {
                                                Ext.Msg.alert("提示", "<font color=red>加载出错,请刷新页面重试!</font>");
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                }),
                new Ext.menu.Item({
                    text: "查看成交信息",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        Ext.Msg.alert('系统提示', '改功能尚未开放');
                    }
                }),
                new Ext.menu.Item({
                    text: "采购结束",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        Ext.Msg.alert('系统提示', '改功能尚未开放');
                    }
                }),

                new Ext.menu.Separator(),
                new Ext.menu.Item({
                    text: "删除",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        Ext.MessageBox.confirm('系统提示', '确认要删除选中记录么？', function (btn) {
                            if (btn !== 'yes') {
                                return;
                            }

                            Ext.Ajax.request({
                                method: 'DELETE',
                                url: '/tender/get?id=' + rec.get('id'),

                                success: function (response) {
                                    grid.getStore().loadPage(1);
                                    Ext.Msg.alert('系统提示', '删除成功！');
                                },

                                failure: function (response) {
                                    Ext.Msg.alert('系统提示', '删除失败!');
                                }
                            });
                        })
                    }
                })
            ]
        });
        treeMenu.showAt(e.getPoint());
    },

    addAction: function (b) {
        var me = this;
        var win = Ext.create("Admin.ux.Window", {
            width: 900,
            height: 700,
            animateTarget: b,
            items: { xtype: 'bidding-edit' },
            buttons: [
                {
                    text: '保存',
                    ui: 'soft-green',
                    iconCls: 'x-fa 	fa-floppy-o',
                    handler: function (btn) {
                        var form = btn.up("window").down("form").getForm();
                        Ext.create('Admin.ux.Action').submit(form, '/tender/sectionAdd', me.getView(), win);
                    }
                },
                {
                    text: '重置',
                    ui: 'soft-red',
                    iconCls: 'x-fa 	fa-mail-reply',
                    handler: function (btn) {
                        btn.up("window").down("form").reset();
                    }
                },
                {
                    text: '取消',
                    ui: 'gray',
                    iconCls: 'x-fa 	fa-ban',
                    handler: function (btn) {
                        btn.up("window").close();
                    }
                }
            ]
        });
    }

});